﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
using CE.iPhone.PList.Internal;
using System.IO;
namespace cczEdit.DramaDialog
{
    public partial class AddDramaDialog : Form
    {
        PListDict rootDic;
        bool isLogicalTest;
        string lastInstruct;
        string lastLogicalTest;
        public AddDramaDialog()
        {
            InitializeComponent();
            PListRoot root = PListRoot.Load(Form1.cczDirector + "\\DramaPrototype.plist");
            rootDic = (PListDict)root.Root;
            foreach (KeyValuePair<int, String> kv in Constant.instructTypeDic)
            {
                comboBox1.Items.Add(kv.Value.ToString());
            }
            
            
        }
        public void Show(IWin32Window owner, bool isLogicalTestInstruct)
        {
            comboBox1.Items.Clear();
            isLogicalTest = isLogicalTestInstruct;

            if (isLogicalTestInstruct)
            {
                foreach (KeyValuePair<int, String> kv in Constant.logicalTestType)
                {
                    comboBox1.Items.Add(kv.Value.ToString());
                }
                comboBox1.SelectedItem = lastLogicalTest;
            }
            else
            {
                foreach (KeyValuePair<int, String> kv in Constant.instructTypeDic)
                {
                    comboBox1.Items.Add(kv.Value.ToString());
                }
                comboBox1.SelectedItem = lastInstruct;
            }
            ShowDialog(owner);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int key = -1;
            
            foreach (KeyValuePair<int, String> kv in isLogicalTest?Constant.logicalTestType:Constant.instructTypeDic)
            {
                if (comboBox1.SelectedItem.ToString() == kv.Value)
                {
                    key = kv.Key;
                    break;
                }
            }

            Form1 mainForm = (Form1)this.Owner;
            
            if (isLogicalTest)
            {
                PListDict instructDict = CreateConditionDict((kLogicalTestType)key);
                TreeNode node = CreateConditionTreeNode(instructDict);
                mainForm.ucDrama.addDramaCode(instructDict, node);
                lastLogicalTest = comboBox1.SelectedItem.ToString(); 
            }
            else
            {
                PListDict instructDict = CreateInstructDict((kInstructType)key);
                TreeNode node = CreateTreeNode(instructDict);
                mainForm.ucDrama.addDramaCode(instructDict, node);
                lastInstruct = comboBox1.SelectedItem.ToString();
            }
            this.Hide();
        }
        public static PListDict CreateConditionDict(kLogicalTestType type)
        {
            PListRoot root = PListRoot.Load(Form1.cczDirector + "\\DramaPrototype.plist");
            PListDict rootDic = (PListDict)root.Root;
            PListDict dramaPrototype = (PListDict)rootDic["Triggers"];
            foreach (KeyValuePair<String, IPListElement> kv in dramaPrototype)
            {
                PListDict a = (PListDict)kv.Value;
                PListInteger instructType = (PListInteger)a["TestType"];
                if (instructType.Value == (long)type)
                {
                    return a;
                }

            }
            return null;

        }

        public static TreeNode CreateConditionTreeNode(PListDict dict)
        {
            int i = 0;
            PListInteger instructType = (PListInteger)dict["TestType"];
            TreeNode node = new TreeNode(Constant.logicalTestType[(int)instructType.Value]);
            foreach (KeyValuePair<string, IPListElement> kv in dict)
            {
                if (kv.Key == "TestType")
                    continue;
                if (i >= 4)
                    break;
                node.Text += GetNodeDataString(kv.Value, kv.Key);
                ++i;
            }
            return node;
        }
        public static PListDict CreateInstructDict(kInstructType type)
        {
            PListRoot root = PListRoot.Load(Form1.cczDirector + "\\DramaPrototype.plist");
            PListDict rootDic = (PListDict)root.Root;
            PListDict dramaPrototype = (PListDict)rootDic["Drama"];
            foreach (KeyValuePair<String, IPListElement> kv in dramaPrototype)
            {
                PListDict a = (PListDict)kv.Value;
                PListInteger instructType = (PListInteger)a["InstructType"];
                if (instructType.Value == (long)type)
                {
                    
                    return a;
                }
                
            }
            return null;
                
        }

        public static TreeNode CreateTreeNode(PListDict dict)
        {
            PListInteger instructType = (PListInteger)dict["InstructType"];
            TreeNode node = new TreeNode(Constant.instructTypeDic[(int)instructType.Value]);
            if (instructType.Value == (long)kInstructType.kInstructTypeVariableTest
                || instructType.Value == (long)kInstructType.kInstructTypeAmbitionTest)
            {
                TreeNode conditionNode = new TreeNode("条件");
                node.Nodes.Add(conditionNode);
                TreeNode dramasNode = new TreeNode("SubDrama1");
                node.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)dict["SubDrama1"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
                dramasNode = new TreeNode("SubDrama2");
                node.Nodes.Add(dramasNode);
                dramaArr = (PListArray)dict["SubDrama2"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            else if (instructType.Value == (long)kInstructType.kInstructTypeConditionTest)
            {
                TreeNode info = new TreeNode("内部信息:" + ((PListString)dict["Information"]).Value);
                node.Nodes.Add(info);
                TreeNode conditionNode = new TreeNode("Condition");
                node.Nodes.Add(conditionNode);

                PListArray conditionArr = (PListArray)dict["Condition"];
                foreach (PListDict instrctDic in conditionArr)
                {
                    long type = ((PListInteger)instrctDic["TestType"]).Value;

                    TreeNode instructNode = new TreeNode(" " + Constant.logicalTestType[(int)type]);
                    conditionNode.Nodes.Add(instructNode);
                }
                TreeNode dramasNode = new TreeNode("SubDrama1");
                node.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)dict["SubDrama1"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
                dramasNode = new TreeNode("SubDrama2");
                node.Nodes.Add(dramasNode);
                dramaArr = (PListArray)dict["SubDrama2"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            else if (instructType.Value == (long)kInstructType.kInstructTypeInteraction)
            {
                TreeNode conditionNode = new TreeNode("条件");
                node.Nodes.Add(conditionNode);

                TreeNode dramasNode = new TreeNode("SubDrama1");
                node.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)dict["SubDrama1"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
                dramasNode = new TreeNode("SubDrama2");
                node.Nodes.Add(dramasNode);
                dramaArr = (PListArray)dict["SubDrama2"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
                dramasNode = new TreeNode("SubDrama3");
                node.Nodes.Add(dramasNode);
                dramaArr = (PListArray)dict["SubDrama3"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            else
            {

                foreach (KeyValuePair<string, IPListElement> kv in dict)
                {
                    if (kv.Key == "InstructType")
                        continue;
                    node.Text += GetNodeDataString(kv.Value, kv.Key);

                }
            }
            return node;
        }
        static public string GetNodeDataString(IPListElement elem,string key)
        {
            string result = "   ";
            if (elem is PListArray)
            {
                PListArray values = (PListArray)elem;
                foreach (IPListElement item in values)
                {
                    if(item is PListInteger)
                        result += ((PListInteger)item).Value + ",";

                }
            }
            else if (elem is PListBool)
            {
                PListBool values = (PListBool)elem;
                
                if (key == "IsEnemy")
                {
                    result += values.Value ? " 敌方武将" : " 我方武将";
                }else
                    result += values.Value;
            }
            else if (elem is PListInteger)
            {
                PListInteger values = (PListInteger)elem;
                if ((key == "UnitID"||key.Contains("UnitID"))&&values.Value!=-1)
                {
                    Hero hero = UCDrama.heros.Find((h) => { return h.num == values.Value; });
                    if (hero != null)
                        result += hero.name;
                    else
                        result += values.Value;
                }
                else if (key.Contains("X"))
                {
                    result += "(" + values.Value;
                }
                else if (key.Contains("Y"))
                {
                    result = "," + values.Value + ")";
                }
                else if (key == "ArenaActionTag")
                {
                    result += Constant.arenaActionTag[(int)values.Value];
                }
                else if (key == "ArenaAttack1ActionTag")
                {
                    result += Constant.arenaAttack1ActionTag[(int)values.Value];
                }
                else if (key == "ArenaAttack2ActionTag")
                {
                    result += Constant.arenaAttack2ActionTag[(int)values.Value];
                }
                else if (key == "Direction")
                {
                    switch (values.Value)
                    {
                        case 0: result += "东"; break;
                        case 1: result += "西"; break;
                        case 2: result += "南"; break;
                        case 3: result += "北"; break;
                    }
                }
                else if (key == "SymbolType")
                {
                    result += Constant.symbolType[(int)values.Value];
                }
                else if (key == "RActionType")
                {
                    result += Constant.rDramaActionTag[(int)values.Value];

                }
                else if (key == "ObjectType")
                {
                    switch (values.Value)
                    {
                        case 0: result += "火"; break;
                        case 1: result += "船"; break;
                        case 2: result += "起火船"; break;
                        case 3: result += "城门"; break;
                    }
                }
                else if (key == "TileType")
                {
                    result += Constant.terrainDic[(int)values.Value];
                }

                else if (key == "AttributeType")
                {
                    result += Constant.attributeType[(int)values.Value];
                }
                else if (key == "SActionType")
                {
                    result += Constant.sDramaActionTag[(int)values.Value];
                }
                else if (key == "ItemType")
                {
                    switch (values.Value)
                    {
                        case 0: result += "消耗品"; break;
                        case 1: result += "武器"; break;
                        case 2: result += "防具"; break;
                        case 3: result += "辅助装备"; break;
                    }
                }
                else if (key == "MessageBoxType")
                {
                    result += Constant.messageBoxType[(int)values.Value];
                }
                else if (key == "Owner")
                {
                    switch (values.Value)
                    {
                        case 0: result += "我军"; break;
                        case 1: result += "友军"; break;
                        case 2: result += "敌军"; break;
                        case 3: result += "援军"; break;
                        case 4: result += "我军及友军"; break;
                        case 5: result += "敌军及援军"; break;
                        case 6: result += "所有部队"; break;
                    }
                }
                else
                    result += values.Value;
            }
            else if (elem is PListString)
            {
                PListString values = (PListString)elem;
                result += values.Value;
            }
            return result;
        }

        internal static PListDict CreateInstructDict(kLogicalTestType kLogicalTestType)
        {
            throw new NotImplementedException();
        }
    }
}
